<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>模拟一个数据监测</title>
  </head>

  <body>
    <script>
      let data = {
        name: "南京财经大学",
        address: "文苑路3号",
      };

      // 创建一个监视的实例对象，用于监视data中属性的变化
      const ob = new Observer(data);
      // console.log(ob);

      // 准备一个vm实例对象
      let vm = {};
      vm._data = data = ob;

      function Observer(obj) {
        // 汇总对象中所有属性形成一个数组
        const keys = Object.keys(obj);
        // 遍历
        keys.forEach((k) => {
          Object.defineProperty(this, k, {
            get() {
              return obj[k];
            },
            set(v) {
              // 重新解析模板
              obj[k] = v;
            },
          });
        });
      }
    </script>
  </body>
</html>
